﻿
/*
NAILSUPERSTORE ® COMPANY
 
[HISTORY]
Created By  	: NGUYEN VAN TY
Created Date	: 12/07/2010
Modified By     :
Modified Date   :
Note            :

*/

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CommonTagTime;
using CommonTagTime.ModelObject;
using CommonTagTime.Business;
namespace CommonTagTime.DataAccess
{
    public class daoProductCategory
    {
        public daoProductCategory()
        {

        }

        public bool Insert(ProductCategory objProductCategory)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[2];          
            try
            {
                para[0] = new SqlParameter(busProductCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = objProductCategory.CategoryID;

                para[1] = new SqlParameter(busImageProduct.PRODUCTID, SqlDbType.Int);
                para[1].Value = objProductCategory.ProductID;
                result = clsSQL.SPExecute("ProductCategory_Insert", para);               

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoProductCategory", "Insert(ProductCategory objProductCategory)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public ProductCategoryCollection GetListByProductID(int productID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            ProductCategoryCollection items = new ProductCategoryCollection();

            try
            {
                para[0] = new SqlParameter(busProductCategory.PRODUCTID, SqlDbType.Int);
                para[0].Value =productID;
                de = clsSQL.SPDataReader("ProductCategory_GetListByProductID", para);
                while (de.Read())
                {
                    ProductCategory item = Load(de);
                    items.Add(item);
                }
             
                de.Close();               
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("ProductCategory", "List(GetListByProductID)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }
        private ProductCategory Load(IDataReader de)
        {
            ProductCategory item = new ProductCategory();            
            item.CategoryID = clsConvert.DbToInt("CategoryID", de);
            item.ProductID = clsConvert.DbToInt("ProductID", de);
            return item;
            // de.P
            //int intLogID = Convert.ToInt32(_comm.Parameters["@LogID"].Value);
        }
        public bool DeleteByProduct(int productId)
        {
            int result = 0;
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            try
            {
                para[0] = new SqlParameter(busProductCategory.PRODUCTID, SqlDbType.Int);
                para[0].Value = productId;
                result = clsSQL.SPExecute("ProductCategory_DeleteByProductID", para);

            }
            catch (Exception ex)
            {
                clsFile.WriteLog("daoProductCategory", "DeleteByProduct(int productId)", ex.ToString());
            }
            finally
            {
                clsSQL.Dispose();
                clsSQL = null;
            }
            if (result > 0)
                return true;
            return false;
        }
        public ProductCategoryCollection GetListByCategoryID(int cateID)
        {
            clsSQL clsSQL = new clsSQL();
            SqlParameter[] para = new SqlParameter[1];
            SqlDataReader de;
            ProductCategoryCollection items = new ProductCategoryCollection();

            try
            {
                para[0] = new SqlParameter(busProductCategory.CATEGORYID, SqlDbType.Int);
                para[0].Value = cateID;
                de = clsSQL.SPDataReader("ProductCategory_GetListByCategoryId", para);
                while (de.Read())
                {
                    ProductCategory item = Load(de);
                    items.Add(item);
                }

                de.Close();
                de.Dispose();
            }
            catch (Exception e)
            {
                clsFile.WriteLog("daoProductCategory", "GetListByProductID(int productID)", e.ToString());
            }
            finally
            {
                de = null;
                clsSQL.Dispose();
                clsSQL = null;
            }

            return items;
        }
    }
}
